package com.kun.portal.webframe.service;

import com.kun.portal.webframe.dao.GpDao;
import com.kun.portal.webframe.po.ContractManagePo;
import com.kun.portal.webframe.po.GpConfigPo;
import com.kun.portal.webframe.util.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TemporalType;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018-09-05.
 * 停机位配置业务层
 */
@Service
public class GpService {
    @Autowired
    private GpDao gpDao;

    @Autowired
    private EntityManager entityManager;
    public List<GpConfigPo> findAllGp(){
        Sort sort = new Sort(new Sort.Order(Sort.Direction.ASC,"apron"));

        return gpDao.findAll(sort);
    }
    public List<GpConfigPo> findAllGpWidthCurrentFlight(){
       Query query = entityManager.createQuery("select g from GpConfigPo g left  join fetch  g.currentFlight order by  g.apron");
       return  query.getResultList();
    }
    public List<GpConfigPo> findAllGpWidthCurrentFlightAndGatePOD(){
        Query query = entityManager.createQuery("select distinct g from GpConfigPo g left  join fetch  g.currentFlight left join fetch g.gatePODs pod left join fetch  pod.flightInfoPo where  pod.distributionTime >= :CTime" +
                " or pod.occupyTime >= :CTime or  pod.leaveTime >= :CTime  or pod is null   order by  g.apron");
        Date date =  new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.MILLISECOND,0);
        System.out.println(calendar.getTime().getTime());
        query.setParameter("CTime",calendar.getTime(), TemporalType.TIMESTAMP );
        return  query.getResultList();
    }
    public GpConfigPo saveGp(GpConfigPo gpConfigPo){
      return gpDao.save(gpConfigPo);
    }
    public void deleteGp(GpConfigPo gpConfigPo){
        gpDao.delete(gpConfigPo);
    }
    /*获取未分配未占用的机位*/
    public List<GpConfigPo> findNoUseGp(){
      return gpDao.findByRunStatusEquals(Status.AerodromeStatus.FREE);
    }
    public GpConfigPo findByOne(String gpId){
        return  gpDao.findOne(gpId);

    }


}
